﻿using System;
using System.Collections.Generic;
using System.Text;

using Asp = System.Web;

namespace Hanhud.Web.UI
{
    public class HANHUDNavigator : Asp.UI.Control
    {
        private List<CMenuItem> _menuItems = null;
        public IEnumerable<CMenuItem> MenuItems
        {
            get { return _menuItems; }
        }

        private CMenuItem _selectedItem = null;
        public CMenuItem SelectedItem
        {
            get { return _selectedItem; }
            set { _selectedItem = value; }
        }

        public HANHUDNavigator()
        {
            this.Load += new EventHandler(DACONavigator_Load);
        }

        void DACONavigator_Load(object sender, EventArgs e)
        {
            //if(!Page.IsPostBack && !Page.IsCallback)
                BindMenuTree();
        }

        private void BindMenuTree()
        {
            _menuItems = CWeb.GetMenus();
        }

        protected override void Render(Asp.UI.HtmlTextWriter writer)
        {
            //base.Render(writer);
            writer.WriteLine(@"
                <!-- Begin navigator -->
                <div class=""MenuTop"">
                    <div class=""LineBottomMenu"">
                        <div class=""QuickMenu"">");

            RenderMenus(writer, MenuItems);
                
            writer.WriteLine(@"
                        </div>
                    </div>
                </div>
                <!-- End navigator -->
            ");
        }

        void RenderMenus(Asp.UI.HtmlTextWriter writer, IEnumerable<CMenuItem> menuItems)
        {
            writer.WriteLine("<ul>");
            foreach (CMenuItem item in menuItems)
            {
                writer.WriteLine(@"<li id=""{3}"">
                    <a href=""{0}"" title=""{1}""><span>{2}</span></a>",Page.ResolveUrl(item.RelativeUrl), item.Title, item.Name, item.ClientID);

                if (item.ChildMenuItems.Count > 0)
                    RenderMenus(writer, item.ChildMenuItems);

                writer.WriteLine("</li>");
            }
            writer.WriteLine("</ul>");
        }
    }
}
